package nuffzone;

import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class noteservlet extends HttpServlet {
	
	private static int idCounter = 0;
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		com.google.appengine.api.users.User user = userService.getCurrentUser();
		noteManager NM = noteManager.getInstance();
		if (user == null) {
		} else {
			String Username = user.getNickname();
			String Note = req.getParameter("note");
			String Priority = req.getParameter("priority");
			String Id = getNewUniqueId();
			NM.createNote(Username, Note, Priority, Id);
		}
		resp.sendRedirect("/home.jsp");

	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		com.google.appengine.api.users.User user = userService.getCurrentUser();
		noteManager NM = noteManager.getInstance();
		if (user == null) {
		} else {
			String Username = user.getNickname();
			String Id = req.getParameter("id");

			NM.deleteNote(Username, Id);
		}
		resp.setStatus(200);
		resp.sendRedirect("/home.jsp");
	}
	
	public String getNewUniqueId(){
		idCounter++;
		return idCounter+"";
	}
}